package com.wodwl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wodwl.dao.BlogDao;
import com.wodwl.dao.CategoryDao;
import com.wodwl.dao.BlogDao.Mode;
import com.wodwl.dao.UserDao;
import com.wodwl.entity.Blog;
import com.wodwl.entity.Category;
import com.wodwl.entity.User;

@Controller
public class BlogController {
	private String blog_add_view="/blog/blog_add";
	private String blog_add_success_view="/blog/blog_list";
	private String blog_list_view="/blog/blog_list";
	private String blog_show_view="/blog/blog_show";
	private String blog_editor_view="/blog/blog_editor";
	private String category_list="/blog/category_list";
	
	
	/*
	 * 写json
	 */
	@RequestMapping("/blog/json.do")
	public void jsonTest(HttpServletRequest request,HttpServletResponse response) throws IOException, JSONException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("name","wodwl");
		out.print(jsonObject);
		
	}
//////////////////////////分类相关////////////////////////////////
	@RequestMapping("/blog/category_add.do")
    public ModelAndView addCategory(HttpServletRequest request,HttpServletResponse response) {
		
		User user=(User)request.getSession().getAttribute("g305_user");
		int uid=user.getUid();
		String cname=request.getParameter("cname");
		Category category=new Category();
		category.setUid(uid);
		category.setType(Category.Type.BLOG);
		category.setCname(cname);
		
		CategoryDao dao=new CategoryDao();
		dao.insert(category);
		
		List<Category> categories=dao.getCategorysByUidType(category);
		Map map=new Hashtable();
		map.put("categories", categories);

		return new ModelAndView(category_list,map);
    }
	
	@RequestMapping("/blog/category_list.do")
    public ModelAndView listCategory(HttpServletRequest request,HttpServletResponse response) {
		Category category=new Category();
		User user=(User)request.getSession().getAttribute("g305_user");
		int uid=user.getUid();
		category.setUid(uid);
		category.setType(Category.Type.BLOG);
		CategoryDao dao=new CategoryDao();
		List<Category> categories=dao.getCategorysByUidType(category);
		Map map=new Hashtable();
		map.put("categories", categories);

		return new ModelAndView(category_list,map);
    }
	
	@RequestMapping("/blog/category_modify.do")
	 public ModelAndView categoryModify(HttpServletRequest request,HttpServletResponse response) {
		
		int cid=Integer.parseInt(request.getParameter("cid"));
		String cname=request.getParameter("cname");
	
		Category category=new Category();
		category.setCid(cid);
		category.setCname(cname);
		CategoryDao dao=new CategoryDao();
		dao.modifyCNameByCid(category);
		
		return listCategory(request, response);
		
	}
	
	@RequestMapping("/blog/category_delete.do")
	 public ModelAndView deleteCategory(HttpServletRequest request,HttpServletResponse response) {
		
		int cid=Integer.parseInt(request.getParameter("cid"));
		CategoryDao dao=new CategoryDao();
		dao.deleteByCid(cid);
		BlogDao blogDao=new BlogDao();
		blogDao.delCategory(cid);
		return listCategory(request, response);
		
	}
	

	
	
	////////////////博客相关//////////////////////////////

	/*
	 * 显示博客
	 */
	@RequestMapping("/blog/blog_show.do")
	 public ModelAndView displayInfo(HttpServletRequest request,HttpServletResponse response) {
		Map map=new Hashtable();
		BlogDao dao2=new BlogDao();
		Blog blog=dao2.getBlogByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("blog", blog);
		return new ModelAndView(getBlog_show_view(),map);
		
	}
	
	/*
	 * 编辑博客
	 */
	@RequestMapping("/blog/blog_editor1.do")
	 public ModelAndView editorInfo1(HttpServletRequest request,HttpServletResponse response) {
		CategoryDao dao=new CategoryDao();
		Category category=new Category();
		User user=(User)request.getSession().getAttribute("g305_user");
		int uid=user.getUid();
		category.setUid(uid);
		category.setType(Category.Type.BLOG);
		List<Category>categories=dao.getCategorysByUidType(category);
		
		Map map=new Hashtable();
		BlogDao dao2=new BlogDao();
		Blog blog=dao2.getBlogByBid(Integer.parseInt(request.getParameter("bid")));
		map.put("blog", blog);
		map.put("categories", categories);
		return new ModelAndView(getBlog_editor_view(),map);
	}
	
	
	@RequestMapping("/blog/blog_editor2.do")
	 public ModelAndView editorInfo2(HttpServletRequest request,HttpServletResponse response) {
		
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String username=request.getParameter("username");
		int cid=-1;
		try{
			cid=Integer.parseInt(request.getParameter("cid"));
		}catch (Exception e) {
			cid=-1;
		}
		
		
		
		Blog blog=new Blog();
		blog.setBid(Integer.parseInt(request.getParameter("bid")));
		blog.setTitle(title);
		blog.setContent(content);
		blog.setUsername(username);
		blog.setCid(cid);
		blog.setCreate_time(new Date());
		
		BlogDao dao=new BlogDao();
		dao.update(blog, Mode.UPDATE);
		Map map=new Hashtable();
		map.put("blog", blog);
		
		
		return new ModelAndView(getBlog_show_view(),map);
		
	}
	
	/*
	 * 添加博客
	 */

	@RequestMapping("/blog/blog_add.do")
    public ModelAndView addBlog(HttpServletRequest request,HttpServletResponse response) {
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String username=request.getParameter("username");
		int cid=-1;
		try{
			cid=Integer.parseInt(request.getParameter("cid"));
		}catch (Exception e) {
			cid=-1;
		}
		
		//添加博客
		Blog blog=new Blog();
		blog.setTitle(title);
		blog.setContent(content);
		blog.setUsername(username);
		blog.setCid(cid);
		BlogDao dao=new BlogDao();
		dao.update(blog, Mode.INSERT);
		//相应分类加1
		CategoryDao categoryDao=new CategoryDao();
		categoryDao.addCountByCid(cid);
		
		dao=new BlogDao();
		List<Blog> blogs=dao.getAllBlogs();
		for(int i=0;i<blogs.size();i++){
			blog=blogs.get(i);
			if(blog.getContent()!=null&&blog.getContent().length()>150)
				blog.setContent(blog.getContent().substring(0,151));
		}
		Map map=new Hashtable();
		map.put("blogs", blogs);

		return new ModelAndView(getBlog_list_view(),map);
    }
	
	/*
	 * 博客列表
	 */
	@RequestMapping("/blog/blog_list.do")
    public ModelAndView listBlog(HttpServletRequest request,HttpServletResponse response) throws IOException, JSONException {
		
		
		BlogDao dao=new BlogDao();
		List<Blog> blogs=dao.getAllBlogs();
		for(int i=0;i<blogs.size();i++){
			Blog blog=blogs.get(i);
			if(blog.getContent()!=null&&blog.getContent().length()>150)
				blog.setContent(blog.getContent().substring(0,151));
		}
		Map map=new Hashtable();
		map.put("blogs", blogs);
		return new ModelAndView(getBlog_list_view(),map);
		
		/*
		JSONArray jsonArray=new JSONArray();
		BlogDao dao=new BlogDao();
		List<Blog> blogs=dao.getAllBlogs();
		for(int i=0;i<blogs.size();i++){
			Blog blog=blogs.get(i);
			String str=blog.getContent();
			if(str!=null){
				str=str.replaceAll("<.*>", "");
				str=str.replaceAll("<", "");
				str=str.replaceAll(">", "");
				if(str.length()>200)
					str=str.substring(0,201);
				}
			blog.setContent(str);
			blogs.set(i, blog);
			jsonArray.put(blog.toJsonObject());
		}
		
		
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonArray);
		
        */
    }
	
	@RequestMapping("/blog/blog_add_view.do")
    public String addBlogView(HttpServletRequest request,HttpServletResponse response) {
		CategoryDao dao=new CategoryDao();
		Category category=new Category();
		User user=(User)request.getSession().getAttribute("g305_user");
		int uid=user.getUid();
		category.setUid(uid);
		category.setType(Category.Type.BLOG);
		List<Category>categories=dao.getCategorysByUidType(category);
		request.setAttribute("categories", categories);
		
        return getBlog_add_view();
    }
	
	
	public String getBlog_add_view() {
		return blog_add_view;
	}

	public String getBlog_add_success_view() {
		return blog_add_success_view;
	}

	public String getBlog_list_view() {
		return blog_list_view;
	}

	public String getBlog_show_view() {
		return blog_show_view;
	}


	public String getBlog_editor_view() {
		return blog_editor_view;
	}
	

	
	
}
